package com.jcodecraeer.xrecyclerview.progressindicator.indicator;

import com.jcodecraeer.xrecyclerview.utils.DeviceUtils;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

public class BallClipRotateMultipleIndicator extends BaseIndicatorController{

    private float scaleFloat=1;
    private float degrees;

    @Override
    public void draw(Canvas canvas, Paint paint) {
        paint.setStrokeWidth(3);
        paint.setStyle(Paint.Style.STROKE_STYLE);
        float circleSpacing=12;
        float x=getWidth()/2;
        float y=getHeight()/2;

        canvas.save();

        canvas.translate(x, y);
        canvas.scale(scaleFloat, scaleFloat);
        canvas.rotate(degrees*360,0,0);
        //draw two big arc
        float[] bStartAngles=new float[]{135,-45};
        for (int i = 0; i < 2; i++) {
            RectFloat rectF=new RectFloat(-x+circleSpacing,-y+circleSpacing,x-circleSpacing,y-circleSpacing);
            Arc arc = new Arc();
            arc.setArc(bStartAngles[i], 90, false);
            canvas.drawArc(rectF,arc , paint);
        }

        canvas.restore();
        canvas.translate(x, y);
        canvas.scale(scaleFloat, scaleFloat);
        canvas.rotate(-degrees*360,0,0);
        //draw two small arc
        float[] sStartAngles=new float[]{225,45};
        for (int i = 0; i < 2; i++) {
            RectFloat rectF=new RectFloat(-x/1.8f+circleSpacing,-y/1.8f+circleSpacing,x/1.8f-circleSpacing,y/1.8f-circleSpacing);
            Arc arc = new Arc();
            arc.setArc(sStartAngles[i], 90, false);
            canvas.drawArc(rectF,arc , paint);
        }

    }

    @Override
    public List<Animator> createAnimation() {
        List<Animator> animators=new ArrayList<>();
        AnimatorValue scaleAnim = new AnimatorValue();
        scaleAnim.setDuration(1000);
        scaleAnim.setLoopedCount(-1);
        scaleAnim.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                scaleFloat = v;
                postInvalidate();
            }
        });
        scaleAnim.start();

        AnimatorValue rotateAnim = new AnimatorValue();
        rotateAnim.setDuration(1000);
        rotateAnim.setLoopedCount(-1);
        rotateAnim.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                degrees = v;
                postInvalidate();
            }
        });
        rotateAnim.start();
        animators.add(scaleAnim);
        animators.add(rotateAnim);
        return animators;
    }
}
